package com.jboa.service.impl;

import java.util.List;

import com.jboa.dao.EmployeeDao;
import com.jboa.dao.RoleDao;
import com.jboa.dao.UserRoleDao;
import com.jboa.model.Employee;
import com.jboa.model.Role;
import com.jboa.model.UserRole;
import com.jboa.service.EmployeeBiz;




public class EmployeeBizImpl implements EmployeeBiz{

	private EmployeeDao employeeDao;
	private RoleDao roleDao;
	private UserRoleDao userRoleDao;
	
	
	
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}


	public void setUserRoleDao(UserRoleDao userRoleDao) {
		this.userRoleDao = userRoleDao;
	}


	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}


	public Employee login(String sn, String password) {		
		Employee emp =this.employeeDao.findBySn(sn);	
		return emp;	
	}


	public Employee login(Employee employee) {	
		List list =this.employeeDao.getEmployee(employee);		
		if(list.size()==1){
			return (Employee)list.get(0);
		}else{
			return null;
		}
	}


	public List<Employee> findUsers(String username) {
		String hql = "from Employee where name!=?";
		Object [] values= new Object[]{username};
		return this.employeeDao.getEmployeeByHql(hql,values);
	}
	
	public String changeUR(List<Integer> lrid, Employee u) {
		//必须指定至少一个角色
		if (String.valueOf(lrid.get(0)).equals("ognl.NoConversionPossible")) {
			return "input";
		}
		//删除中间表中之前有但是现在被取消的角色记录
		UserRole ur = new UserRole();
		Role r = new Role();
		for (Integer j : userRoleDao.findRoleIdByUid(u.getSn())) {
			if (!lrid.contains(j)) {
				for (UserRole userRole : userRoleDao.findBy2Properties("role.id", j,
						"user.id", u.getSn())) {//其实此处不用使用循环吧，只有一条记录
					userRoleDao.delete(userRole);
				}
			}
		}
		//在中间表添加新增加的角色
		for (Integer i : lrid) {
			if (userRoleDao.findRoleIdByUid(u.getSn()).contains(i)) {
				continue;
			} else {
				ur = new UserRole();
				ur.setEmployee(u);
				r.setId(i);
				ur.setRole(r);
				userRoleDao.save(ur);
			}
		}
		return "success";
	}

	public Employee findSingleUser(Employee u) {		
		return this.employeeDao.findBySn(u.getSn());
	}
	// 查看用户所属角色
	public List<Integer> getRole(Employee u) {
		if(u.getSn()==null){
			u =employeeDao.findByUsername(u.getName()).get(0);
		}
		return userRoleDao.findRoleIdByUid(u.getSn());
	}


	public void modifyUser(Employee u) {
		this.employeeDao.update(u);
	}

	public String newUser(Employee user, List<Integer> rids) {		
		int size =this.employeeDao.findByUsername(user.getName()).size();
		if(size==0){
			//保存用户
			user.setSn(user.getName());
			this.employeeDao.save(user);
			//保存角色
			for(Integer rid:rids){
				UserRole userRole = new UserRole();
				userRole.setRole(this.roleDao.findById(rid));
				userRole.setEmployee(user);
				this.userRoleDao.save(userRole);
			}
			return "success";
		}
		//用户名存在或其他错误
		return "input";
	}

	public List<Employee> operateUser() {
		// TODO Auto-generated method stub
		return this.employeeDao.findAll2();
	}

	public String regist(Employee u) {
		// TODO Auto-generated method stub
		return null;
	}

	public void removeUser(Employee u) {
		List <UserRole> urList =this.userRoleDao.findByProperty("employee.sn", u.getSn());
		for(UserRole ur:urList){
			this.userRoleDao.delete(ur);
		}
		this.employeeDao.delete(u);
		
	}


}
